package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.domain.mall.MallCollect;
import com.tencreat.domain.mall.dto.CollectAddDTO;
import com.tencreat.domain.mall.vo.ResMallCollectVO;
import com.tencreat.service.mall.IMallCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 *  我的收藏Controller
 *
 * @author wangkai
 * @date 2021-11-15
 */
@Api(tags = "98-我的收藏api")
@RestController
@RequestMapping("/api/collect")
public class MallCollectController extends BaseController {
    @Autowired
    private IMallCollectService mallCollectService;

    @ApiOperation("我的收藏列表")
    @GetMapping("/collectList")
    public HttpResult<ResMallCollectVO> cutIngInfo() {
        return HttpResult.ok(mallCollectService.getResMallCollect(getLoginCheck()));
    }

    @ApiOperation("收藏/取消收藏")
    @PostMapping("/add")
    public HttpResult add(@RequestBody @Validated CollectAddDTO dto) {
        Long uuid = getLoginCheck().getAccountLogin().getUuid();
        MallCollect collect = new MallCollect();
        collect.setUserId(uuid);
        collect.setValueId(dto.getValueId());
        collect.setType(dto.getType());
        try {
            mallCollectService.insertMallCollect(collect);
        } catch (Exception e) {
            mallCollectService.remove(new LambdaQueryWrapper<MallCollect>()
                    .eq(MallCollect::getUserId, uuid)
                    .eq(MallCollect::getValueId, dto.getValueId())
                    .eq(MallCollect::getType, dto.getType()));
        }
        return HttpResult.ok();
    }

    @ApiOperation("该商品是否收藏")
    @GetMapping("/is-collect")
    public HttpResult<Boolean> isCollect(CollectAddDTO dto) {
        Long uuid = getLoginCheck().getAccountLogin().getUuid();
        int count = mallCollectService.lambdaQuery()
                .eq(MallCollect::getUserId, uuid)
                .eq(MallCollect::getType, dto.getType())
                .eq(MallCollect::getValueId, dto.getValueId())
                .count();
        if (count > 0) {
            return HttpResult.ok(Boolean.TRUE);
        }
        return HttpResult.ok(Boolean.FALSE);
    }

}
